package cn.gxufe.reliable.message.producer.service;

import cn.gxufe.reliable.message.api.producer.MessageCallBackInterface;
import cn.gxufe.reliable.message.api.aware.CompensableMessageAware;
import cn.gxufe.reliable.message.api.service.CompensableMessage;
import cn.gxufe.reliable.message.common.Message;
import cn.gxufe.reliable.message.common.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author yingjie.rong
 **/
@Service
public class MessageCallBackInterfaceImpl implements MessageCallBackInterface , CompensableMessageAware {
    private CompensableMessage compensableMessage;
    private static final  Logger logger = LoggerFactory.getLogger(MessageCallBackInterfaceImpl.class);
    /**
     * 消费者（下游服务）执行成功后回调此方法
     * @return
     */
    @Override
    public ResultVo finish(Long messageId, String key, String header,String finishData) {
        logger.info("");
        Message message = compensableMessage.getMessage();
        System.out.println(message + ", name = " + compensableMessage.getValue("name"));
        return ResultVo.ok();
    }

    /**
     *消费者（下游服务）执行失败后回调此方法
     * @return
     */
    @Override
    public ResultVo cancel(Long messageId, String key, String header,String cancelData) {
        System.out.println("cancel : messageId = " + messageId + "finishData = " + cancelData);
        return ResultVo.ok();
    }
    @Override
    public void setCompensableMessage(CompensableMessage compensableMessage) {
        this.compensableMessage = compensableMessage;
    }
}
